package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 获取所有套餐信息（无需分页）
     * @return
     */
    @PostMapping("/getSetmeal")
    @ApiOperation("获取套餐列表")
    public Result getSetmeal(){
        try {
            List<Setmeal> list =setmealService.getSetmeal();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    /**
     * 根据id查询套餐详情
     * @param id
     * @return
     */
    @PostMapping("/findById")
    @ApiOperation("根据id查询套餐详情")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            log.info("根据id查询套餐详情：{},{},{}",setmeal.getId(),setmeal.getName(),setmeal.getAge());
            log.info("查询完毕");
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            log.error(e.getMessage());
            //e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
